/*******************************************************************************
* 
* AudioManager create and recycle AudioSprite
*
*   !!This class is totally deprecated since XAudio crashes most of the time!!
*   Use FMOD instead
* 
******************************************************************************/
#include "audio-manager.h"
#include "logging.h"

#include <xaudio2.h>

using namespace noria::diagnostic;

namespace noria{ namespace xaudio
{
    AudioManager::AudioManager(IXAudio2* pXAudio2)
        : m_XAudio2(NULL), m_master(NULL)
    {
        LOG_INFO("AudioManager::ctor()");
        init(pXAudio2);
    }

    AudioManager::~AudioManager(void)
    {
        if(m_master)
            m_master->DestroyVoice();

        if(m_XAudio2)
            m_XAudio2->Release();

        LOG_INFO("AudioManager::~ctor()");
    }

    unique_ptr<AudioSprite> AudioManager::create(cbinary_ref pcm) const
    {
        unique_ptr<AudioSprite> spirit(new AudioSprite(m_XAudio2, pcm));
        return spirit;
    }

    void AudioManager::init(IXAudio2* pXAudio2)
    {
        m_XAudio2 = pXAudio2;

        HRESULT hr = m_XAudio2->Initialize();
        if(FAILED(hr))
        {
            LOG_FATAL("Failure calling IXAudio2::Initialize. error=%#X", hr);
            goto end;
        }

        hr = m_XAudio2->CreateMasteringVoice( &m_master );
        if(FAILED(hr))
        {
            LOG_FATAL("Failure calling IXAudio2::CreateMasteringVoice. error=%#X", hr);
            m_master = NULL;
            goto end;
        }

        end:;
    }


}}